package jeecg.business.service.impl.send;

import java.util.List;

import jeecg.business.entity.email.NmsSendMailEntity;
import jeecg.business.entity.enter.EnterEntity;
import jeecg.business.entity.send.SendMessEntity;
import jeecg.business.job.SmsTask;
import jeecg.business.service.email.NmsSendMailServiceI;
import jeecg.business.service.send.SendMessServiceI;
import jeecg.business.util.SmsUtil;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("sendMessService")
@Transactional
public class SendMessServiceImpl extends CommonServiceImpl implements SendMessServiceI {
	
	@Autowired
	NmsSendMailServiceI mailService;
	
	public boolean send(SendMessEntity sendMess) throws Exception{
		List<EnterEntity> list = listEnter(sendMess.getId());
		if(list != null && list.size()>0){
			NmsSendMailEntity mail = null;
			/**
			 * 发送邮件
			 */
			if("1".equals(sendMess.getYjfs())){
				for(EnterEntity e : list){
					if(StringUtil.isNotEmpty(e.getEmail())){
						mail = mailService.random();
						SmsTask.send(mail, e.getEmail(), sendMess.getNr());
					}
				}
			}
			
			/**
			 * 发送短信
			 */
			if("1".equals(sendMess.getDxfs())){
				String phone = null;
				for(EnterEntity e : list){
					if(StringUtil.isNotEmpty(e.getMobile())){
						if(phone == null) phone = e.getMobile();
						else phone += ","+e.getMobile();
					}
				}
				if(StringUtil.isNotEmpty(phone))
					SmsUtil.sendSms(phone, sendMess.getNr());
			}
			
			sendMess.setZt("2");
			return true;
		}
		return false;
	}
	
	
	List<EnterEntity> listEnter(String xxid){
		String hql = " select e from EnterEntity e , SendMessEnterEntity s where e.id = s.qyid and s.xxid = '"+xxid+"' ";
		return findByQueryString(hql);
	}
}